package com.ziyucoding.springboot.labs.lab8.skywalking.rocketmqdemo.controller;

import com.ziyucoding.springboot.labs.lab8.skywalking.rocketmqdemo.producer.DemoProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 示例 控制器
 *
 * @author ziyucoding
 * @date 2022-11-01
 **/
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Autowired
    private DemoProducer demoProducer;

    @GetMapping("/hello")
    public Map<String, Object> sayHello() {
        SendResult sendResult = this.sendMessage(1);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "成功");
        result.put("data", sendResult.getSendStatus());
        return result;
    }

    /**
     * 发送消息
     *
     * @param id
     * @return SendResult
     **/
    private SendResult sendMessage(Integer id) {
        return demoProducer.syncSend(id);
    }
}
